<?php
/**
* @Copyright Copyright (C) 2010- Gary Teh Name1price.Com
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class SimpleSWFUploadView
{
	function showUploadForm(){

		$document =& JFactory::getDocument();  

		$document->setBuffer($listStyle, 'modules', 'submenu');		
		$document->title = "Upload a new document";		
		$document->addScript( AssetsURLocation.'jquery/jquery.js' );		
		$document->addScript( AssetsURLocation.'swfupload/swfupload.js?ver=2.9.2' );
		$document->addScript( AssetsURLocation.'swfupload/swfupload.queue.js?ver=2.9.2' );		
		$document->addScript( AssetsURLocation.'swfupload/fileprogress.js?ver=2.9.2' );				
		$document->addScript( AssetsURLocation.'swfupload/handlers.js?ver=2.9.2' );		
		$document->addStyleSheet(AssetsURLocation.'swfupload/default.css');
						
		
		$session = & JFactory::getSession();		
		
		?>
		

		<script>
			jQuery(document).ready(function() {			
				try{
					var settings = {
						flash_url : '<?php echo AssetsURLocation; ?>swfupload/swfupload.swf',
						upload_url: '<?php echo JURI::root(); ?>administrator/components/com_simpleswfupload/uploadhandler.php',
						//upload_url: '<?php echo JURI::root(); ?>administrator/',
						post_params: {
							'format' : 'raw',
							'swfupload' : '1',
							 '<?php echo $session->getName();?>' : '<?php echo $session->getId();?>',	

						},
						file_size_limit : '5 MB',
						file_types : '*.*;', 
						file_types_description : 'All Files',
						file_upload_limit : 100,
						file_queue_limit : 0,
						custom_settings : {
							progressTarget : 'fsUploadProgress',
							cancelButtonId : 'btnCancel'
						},
						debug: false,
			
						// Button settings
						button_image_url: '<?php echo AssetsURLocation; ?>swfupload/upload.png',
						button_width: '132',
						button_height: '24',
						button_placeholder_id: 'spanButtonPlaceHolder',
						button_text: "<span class='button'>Upload a File</span>",
						button_text_style: ".button{ text-align: center; font-weight: bold; font-family:'Lucida Grande','Lucida Sans Unicode',Tahoma,Verdana,sans-serif; }",
						button_text_left_padding: 0,
						button_text_top_padding: 2,
						
						// The event handler functions are defined in handlers.js
						file_queued_handler : fileQueued,
						file_queue_error_handler : fileQueueError,
						file_dialog_complete_handler : fileDialogComplete,
						upload_start_handler : uploadStart,
						upload_progress_handler : uploadProgress,
						upload_error_handler : uploadError,
						upload_success_handler : uploadSuccess,
						upload_complete_handler : uploadComplete,
						queue_complete_handler : queueComplete	// Queue plugin event
					};		
					
					//alert(swfu );
					if(typeof swfu == "undefined"){
						swfu = new SWFUpload(settings);
						
						/*
						//echos all the post parameters of this object
						for(var i in swfu.settings.post_params){
							alert(swfu.settings.post_params[i])
						}
						*/
					}
				}
				catch(exception){
					alert('Cannot upload xml this time '+exception);
				}	
			});		
		</script>		
			<div align='center'>


				<div style='height:10px;'></div>
			
				<div id='divStatus'></div>
				<div class='fieldset flash' id='fsUploadProgress'>
					<span class='legend' style='display:none;'>Upload Queue</span>
				</div>

				<div id='divStatus' style='display:none;'>0 Files Uploaded</div>

			
				<div class='flash-image-uploader'>
					<span id='spanButtonPlaceHolder'></span>
				<div id='media-items-library'> </div>
				</div>		
			
	
			</div>

		
		<?php	
	}
    
    function uploadEmbbeded(){
        $document    = & JFactory::getDocument();
               
        $document->addScript( AssetsURLocation.'jquery/jquery.js' );
        $document->addScript( AssetsURLocation.'swfupload/swfupload.js?ver=2.9.2' );
        $document->addScript( AssetsURLocation.'swfupload/swfupload.queue.js?ver=2.9.2' );        
        $document->addScript( AssetsURLocation.'swfupload/fileprogress.js?ver=2.9.2' );                
        $document->addScript( AssetsURLocation.'swfupload/handlers.js?ver=2.9.2' );        
        $document->addStyleSheet(AssetsURLocation.'swfupload/default.css');
        
        $session = & JFactory::getSession();                
    ?>
       
        <script type="text/javascript">
            jQuery(document).ready(function() {            
                try{
                    var settings = {
                        flash_url : '<?php echo AssetsURLocation; ?>swfupload/swfupload.swf',
                        upload_url: '<?php echo JURI::root(); ?>administrator/components/com_simpleswfupload/uploadhandler.php',
                        //upload_url: '<?php echo JURI::root(); ?>administrator/',
                        post_params: {
                            'format' : 'raw',
                            'swfupload' : '1',
                             '<?php echo $session->getName();?>' : '<?php echo $session->getId();?>',    

                        },
                        file_size_limit : '5 MB',
                        file_types : '*.*;', 
                        file_types_description : 'All Files',
                        file_upload_limit : 100,
                        file_queue_limit : 0,
                        custom_settings : {
                            progressTarget : 'fsUploadProgress',
                            cancelButtonId : 'btnCancel'
                        },
                        debug: false,
            
                        // Button settings
                        button_image_url: '<?php echo AssetsURLocation; ?>swfupload/upload.png',
                        button_width: '132',
                        button_height: '24',
                        button_placeholder_id: 'spanButtonPlaceHolder',
                        button_text: "<span class='button'>Upload a File</span>",
                        button_text_style: ".button{ text-align: center; font-weight: bold; font-family:'Lucida Grande','Lucida Sans Unicode',Tahoma,Verdana,sans-serif; }",
                        button_text_left_padding: 0,
                        button_text_top_padding: 2,
                        
                        // The event handler functions are defined in handlers.js
                        file_queued_handler : fileQueued,
                        file_queue_error_handler : fileQueueError,
                        file_dialog_complete_handler : fileDialogComplete,
                        upload_start_handler : uploadStart,
                        upload_progress_handler : uploadProgress,
                        upload_error_handler : uploadError,
                        upload_success_handler : uploadSuccess,
                        upload_complete_handler : uploadComplete,
                        queue_complete_handler : queueComplete,    // Queue plugin event
                        
                        return_element : '<?php echo $current_id = JRequest::getVar('element_name', '', 'get'); ?>'
                    };        
                    
                    //alert(swfu );
                    if(typeof swfu == "undefined"){
                        swfu = new SWFUpload(settings);
                        
                        /*
                        //echos all the post parameters of this object
                        for(var i in swfu.settings.post_params){
                            alert(swfu.settings.post_params[i])
                        }
                        */
                    }
                }
                catch(exception){
                    alert('Cannot upload xml this time '+exception);
                }    
            });    

            function uploadSuccess(file, serverData) {                    
                try {                                                                
                    //var result = jQuery.parseJSON('"' + serverData + '"');   
                    var result = eval("(" + serverData + ")");
                    var progress = new FileProgress(file, this.customSettings.progressTarget);
                    
                    progress.setStatus(result.status);
                    progress.setStatus(result.msg);
                    progress.toggleCancel(false);
                    if(result.success == 1){
                        progress.setComplete(); 
                        selectFile(result.file);
                        //window.parent.document.getElementById('<?php echo $current_id = JRequest::getVar('element_name', '', 'get'); ?>').value = result.file;              
                    }
                    else{
                        progress.setError();   
                    } 
                    
                    //alert(result.msg);                     
                    //eval(serverData);     
                } catch (ex) {
                    this.debug(ex);
                }
            }

            function selectFile(file) { 
                window.parent.document.getElementById('<?php echo $current_id = JRequest::getVar('element_name', '', 'get'); ?>').value = file;                
                window.parent.document.getElementById('sbox-window').close();
            }
        </script>   
        
        <div align="center">
            <?php 
            require_once( JPATH_COMPONENT.DS.'configuration.php' );                
            $files = JFolder::files(DOWNLOAD_PATH); 
            ?>
            <div style="width: 600px;">
                <table cellspacing="1" class="adminlist flash">
                    <thead>
                        <tr>     
                            <th class="title">Title</th>
                            <th width="10%" nowrap="nowrap">Select</th>                        
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <td colspan="2">
                            </td>
                        </tr>
                    </tfoot>
                    <tbody>
                        <?php
                        $i = 0; 
                        foreach($files as $item){ 
                            $file_name = 'images/stories/downloads/' . $item;
                            $i++;
                            $i %= 2; 
                            ?>
                            <tr class="row<?php echo $i; ?>">  
                                <td align="left"><?php echo $file_name; ?></td>                      
                                <td align="center">
                                    <span class="editlinktip hasTip">
                                        <button onclick="return selectFile('<?php echo $file_name; ?>');" type="button">Select</button>
                                        <!--
                                        <a onclick="return selectFile('<?php echo $file_name; ?>');" href="javascript:void(0);">
                                            <img width="16" height="16" border="0" alt="Select" src="images/publish_g.png">
                                        </a>
                                        -->
                                    </span>
                                </td>  
                            </tr> 
                        <?php } ?>                              
                    </tbody>
                </table>  
            </div>  
        </div>        
        <div align="center" class="upload">
            <div style='height:10px;'></div>        
            <div id='divStatus'></div>
            <div class='fieldset flash' id='fsUploadProgress'>
                <span class='legend' style='display:none;'>Upload Queue</span>
            </div>
            <div id='divStatus' style='display:none;'>0 Files Uploaded</div>        
            <div class='flash-image-uploader'>
                <span id='spanButtonPlaceHolder'></span>
                <div id='media-items-library'> </div>
            </div>        
        </div>
       
       <?php
    }
}
?>